package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.HealthAdvice;
import com.itheima.entity.Result;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.MemberInfo;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //保存会员信息
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override//更新会员信息
    public Result update(Member member) {
        try {
            memberDao.edit(member);
            return new Result(true, MessageConstant.UPDATE_MEMBER_INFO_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_MEMBER_INFO_FAIL);
        }
    }

    @Override//通过会员id查询相关健康信息
    public HealthAdvice findHealthAdvice(Integer id) {
        HealthAdvice healthAdvice = memberDao.findHealthAdvice(id);
        if (healthAdvice==null) {
            return null;
        }
        int healthAdviceId = healthAdvice.getId();
        String img = memberDao.findByHealthAdviceId(healthAdviceId);
        String memberName = memberDao.findMemberNameByHealthAdviceId(healthAdviceId);
        List<String> sports = memberDao.findSports(healthAdviceId);
        List<String> foods = memberDao.findFoods(healthAdviceId);
        healthAdvice.setMemberName(memberName);
        healthAdvice.setImg(img);
        healthAdvice.setSports(sports);
        healthAdvice.setFoods(foods);
        return healthAdvice;
    }


    //会员分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);
        //select * from member limit 0,10
        Page<Member> page = memberDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Member> rows = page.getResult();
        for (Member member : rows) {
            //修改性别
            String sex = member.getSex();
            if(sex != null) {
                if ("1".equals(sex)){
                    member.setSex("男");
                } else {
                    member.setSex("女");
                }
            } else {
                member.setSex("未知");
            }
            //计算年龄
            String birthday = null;
            try {
                birthday = DateUtils.parseDate2String(member.getBirthday());
            } catch (Exception e) {
                e.printStackTrace();
            }
            Integer ageByBirthday = DateUtils.getAgeByBirthday(birthday);
            member.setAge(ageByBirthday);
        }
        return new PageResult(total,rows);
    }


    //根据ID删除会员
    public void deleteById(Integer id) {
        //判断当前会员是否已经关联
        long count = memberDao.findCountByCheckItemId(id);
        if(count > 0){
            //不允许删除
            new RuntimeException();
        }
        memberDao.deleteById(id);
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    @Override
    public List<MemberInfo> findByIds(Integer[] ids) {
        List<MemberInfo> list = new ArrayList<>();
        for (Integer id : ids) {
            List<MemberInfo> memberinfos = memberDao.findByIds(id);
            for (MemberInfo memberinfo : memberinfos) {
                String sex = memberinfo.getSex();
                if(sex != null) {
                    if ("1".equals(sex)){
                        memberinfo.setSex("男");
                    } else {
                        memberinfo.setSex("女");
                    }
                } else {
                    memberinfo.setSex("未知");
                }
                list.add(memberinfo);
            }
        }
        return list;
    }

    @Override
    public Member findByTel(String phoneNumber) {
        return memberDao.findByTel(phoneNumber);
    }

    @Override
    public List<Integer> findOrderByMemId(Integer id) {
        return memberDao.findOrderByMemId(id);
    }

    @Override
    public Member findByFileNumber(String fileNumber) {
        return memberDao.findByFileNumber(fileNumber);
    }
}
